<template>
  <el-card shadow="hover" class="metric-card">
    <div class="card-content">
      <div class="card-icon">
        <el-icon :size="30">
          <component :is="icon" />
        </el-icon>
      </div>
      <div class="card-info">
        <div class="card-title">{{ title }}</div>
        <div class="card-value">
          {{ value }}<span v-if="unit" class="unit">{{ unit }}</span>
        </div>
        <div class="card-trend" :class="trendClass">
          <el-icon :size="14">
            <CaretTop v-if="trend.startsWith('+') || (!trend.startsWith('+') && !trend.startsWith('-'))" />
            <CaretBottom v-else />
          </el-icon>
          {{ trend }}
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { CaretTop, CaretBottom } from '@element-plus/icons-vue'

const props = defineProps({
  title: {
    type: String,
    required: true
  },
  value: {
    type: [String, Number],
    required: true
  },
  unit: {
    type: String,
    default: ''
  },
  trend: {
    type: String,
    default: '0%'
  },
  icon: {
    type: String,
    required: true
  }
})

const trendClass = computed(() => {
  return props.trend.startsWith('-') ? 'negative' : 'positive'
})
</script>

<style scoped>
.metric-card {
  height: 100%;

  .card-content {
    display: flex;
    align-items: center;
    height: 100%;
  }

  .card-icon {
    margin-right: 15px;
    color: var(--el-color-primary);
  }

  .card-title {
    font-size: 14px;
    color: #666;
    margin-bottom: 5px;
  }

  .card-value {
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 5px;

    .unit {
      font-size: 14px;
      margin-left: 2px;
    }
  }

  .card-trend {
    font-size: 12px;
    display: flex;
    align-items: center;

    &.positive {
      color: #67c23a;
    }

    &.negative {
      color: #f56c6c;
    }
  }
}
</style>